
package combat.dudes.server.protocol.implementation;

import java.rmi.RemoteException;
import java.util.ArrayList;

import combat.dudes.protocol.data.DoublePoint;
import combat.dudes.protocol.data.PhysicalObject;
import combat.dudes.protocol.data.RemoteList;
import combat.dudes.protocol.interfaces.OnboardSensorSystemInt;
import combat.dudes.protocol.interfaces.OnboardSystemInt;
import combat.dudes.protocol.interfaces.ProjectileInt;
import combat.dudes.protocol.interfaces.TeamInt;
import combat.dudes.protocol.interfaces.VehicleInt;
import combat.dudes.protocol.types.OnboardSystemStatus;
import combat.dudes.protocol.types.OnboardSystemType;
import combat.dudes.protocol.types.SensorSystemType;
import combat.dudes.server.ServerApp;
import combat.dudes.server.classes.Calculation;


/**
 * @author Laurens Andriessen
 * @author Jesper Keijzer
 * @version 1.0
 */
public class OnboardSensorSystem extends OnboardSystem implements OnboardSensorSystemInt {

	private static final long serialVersionUID = 1L;
	private int range = 40;
	private SensorSystemType sensorSystemType = SensorSystemType.UNKNOWN;

	public OnboardSensorSystem() {
		super(OnboardSystemStatus.ONLINE, OnboardSystemType.SENSOR);
	}

	public OnboardSensorSystem(int range, SensorSystemType sensorSystemType) {
		super(OnboardSystemStatus.ONLINE, OnboardSystemType.SENSOR);
		setRange(range);
		setSensorSystemType(sensorSystemType);
	}

	@Override
	public int getRange() {
		return range;
	}

	@Override
	public void setRange(int range) {
		this.range = range;
	}

	@Override
	public SensorSystemType getSensorSystemType() {
		return sensorSystemType;
	}

	@Override
	public void setSensorSystemType(SensorSystemType sensorSystemType) {
		this.sensorSystemType = sensorSystemType;
	}

	@Override
	public ArrayList<PhysicalObject> scan() { // Remote = PhysicalObject
		ArrayList<PhysicalObject> physicalObjects = new ArrayList<PhysicalObject>();
		// check sensor status
		if(getOnboardSystemStatus().equals(OnboardSystemStatus.ONLINE)) {
			DoublePoint sensorPosition = null;
			// Get sensor(vehicle) position
			for(TeamInt team : Simulation.instance().getTeams())
				for(VehicleInt vehicle : team.getVehicles())
					for(OnboardSystemInt onboardSystem : vehicle.getOnboardSystems())
						if(this.equals(onboardSystem))
														sensorPosition = vehicle.getPosition();

			if(sensorPosition != null) {
				// Scan all vehicles
				for(TeamInt team : Simulation.instance().getTeams())
					for(VehicleInt vehicle : team.getVehicles())
						if(Calculation.MainCalc.distance(sensorPosition, vehicle.getPosition()) < range)
														if(!physicalObjects.contains(vehicle))
																physicalObjects.add((Vehicle) vehicle);
				// Scan all projectiles
				for(TeamInt team : Simulation.instance().getTeams())
					for(ProjectileInt projectile : team.getProjectiles())
						if(Calculation.MainCalc.distance(sensorPosition, projectile.getPosition()) < range)
														if(!physicalObjects.contains(projectile))
																physicalObjects.add((Projectile) projectile);
			}
		}
		return physicalObjects;
	}

	@Override
	public RemoteList scan_Remote() {
		try {
			return new RemoteList(scan());
		} catch(RemoteException e) {
			ServerApp.onError("@OnboardSensorSystem, scan_Remote()", e, false);
		}
		return new RemoteList();
	}
}
